(function($){
    var search = new Object();
    search.init = function(){
        $("#allQues").load(ROOT + "/logined/search/ques.do", function(){
            /* bind change questionnaire event */
            $("#allQues select").bind("change", function(){
                search.changeQues(this.value);
            })
            /* bind search button */
            $("#btnSearch").bind("click", function(){
                search.doSearch();
            })
			
			/* bind list actions
			var links = $(".listActions a");
			$(links[0]).click(function(){
				$("#subjects :checkbox").each(function(){
					this.checked=true;
				});
			});
			$(links[1]).click(function(){
				$("#subjects :checkbox").removeAttr("checked");
			});
			$(links[2]).click(function(){
				$("#subjects :checkbox").each(function(){
					this.checked=!this.checked;
				});
			});
			*/
			
			/* bind advance button */
			$("#adv").bind("click",function(){
				var jq = $("#hiddenSubjects");
				if("true" == jq.attr("show")){
					jq.fadeOut(300,function(){
						jq.find(":checked").each(function(index,ele){
							ele.checked = false;
						});
						jq.attr("show","false");
					});
				}else{
					jq.fadeIn(600,function(){
						jq.attr("show","true");
					});				
				}
			});
			
			/* go to default questionnaire */
            search.changeQues($("#allQues select").val());
            
        });
    };
    
    search.doSearch = function(){
        /* get post object */
        var arr = new Array();
        $("#subjects :checked").each(function(index, ele){
            arr.push(ele.value);
        })
        
        $("#arena").load(ROOT + "/logined/search/search.do", {
            quesId: $("#allQues select").val(),
            keywords: $("#chute input[name='keywords']").val(),
            pagesize: $("#chute input[name='pagesize']").val(),
            pn: $("#chute input[name='pn']").val(),
            subjectIds: arr
        }, function(){
			/* binding pager links */
            $("#arena .pager").each(function(index, ele){
                $(ele).find("a[pn]").one("click", function(){
                    $("#chute input[name='pn']").val($(this).attr("pn"));
                    search.doSearch();
                })
            });
			/* binding children links */
			$("#arena a[reId]").bind("click",function(){
				var me = $(this);
				var pos = me.offset();
				var h = me.height();
				var div = $("#childrenResult");
				div.css("top",pos.top + h + 2);
				div.css("left",pos.left);
				div.load(ROOT + "/logined/search/children.do",{
					parentId : me.attr("reId")
				},function(){
					div.find("img.handle").one("click",function(){
						div.fadeOut(300);
					});
					div.find("a[target='_blank']").one("click",function(){
						div.fadeOut(300);
					})
					div.fadeIn(600,function(){
						
					});
				});
			});
        });
    };
    
    search.changeQues = function(quesId){
        $("#subjects").load(ROOT + "/logined/search/subjects.do", {
            quesId: quesId
        }, function(){
        	
        });
    };
    
    // install search to jQuery
    $.search = search;
})(jQuery);

/*
 * ============================================
 * = main method
 * ============================================
 */
(function($){
    /* main method */
    $(document).ready(function(){
        $.search.init();
    });
})(jQuery);
